package cn.itcast.bos.web.base.action;

import domain.take_delivery.Promotion;
import cn.itcast.bos.service.take_delivery.PromotionService;
import com.opensymphony.xwork2.ActionContext;
import domain.utils.Constants;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import javax.ws.rs.core.MediaType;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Administrator on 2017/7/21/021.
 */
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class PromotionAction extends BaseAction<Promotion,PromotionService>{

    private File titleImgFile;
    private String titleImgFileFileName;
    private String titleImgFileContentType;

    private Integer rows;
    private Integer page;

    @Action(value = "promotion_save",results = {
            @Result(name = "success",type = "redirect",location = "./pages/take_delivery/promotion.html")
    })
    public String save() throws IOException {

        String savepath = ServletActionContext.getServletContext().getRealPath("/upload");
        String saveurl = ServletActionContext.getServletContext().getContextPath() + "/upload";
        UUID uuid = UUID.randomUUID();
        String ext = titleImgFileFileName.substring(titleImgFileFileName.lastIndexOf("."));
        String randomFileName = uuid+ext;

        File destFile = new File(savepath+"/"+randomFileName);
        FileUtils.copyFile(titleImgFile,destFile);
        model.setTitleImg(ServletActionContext.getRequest().getContextPath()+"/upload/"+randomFileName);
        service.save(model);
        return SUCCESS;
    }

    @Action(value = "promotion_pageQuery",results = {
            @Result(name = "success",type = "json")
    })
    public String pageQuery(){
        Pageable pageable = new PageRequest(page-1,rows);
        Page<Promotion> all =service.findAll(pageable);
        ActionContext.getContext().getValueStack().push(all);
        return SUCCESS;
    }


    public File getTitleImgFile() {
        return titleImgFile;
    }

    public void setTitleImgFile(File titleImgFile) {
        this.titleImgFile = titleImgFile;
    }

    public String getTitleImgFileFileName() {
        return titleImgFileFileName;
    }

    public void setTitleImgFileFileName(String titleImgFileFileName) {
        this.titleImgFileFileName = titleImgFileFileName;
    }

    public String getTitleImgFileContentType() {
        return titleImgFileContentType;
    }

    public void setTitleImgFileContentType(String titleImgFileContentType) {
        this.titleImgFileContentType = titleImgFileContentType;
    }

    public Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }
}
